﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WorldGenerator : MonoBehaviour {

	public GameObject TilePrefab;

	
	private Sprite[] worldTiles;
	Dictionary<string, int> worldTileNames;

	// Use this for initialization
	void Start () {

		
		// setup the sprite list
		worldTiles = Resources.LoadAll<Sprite>("Terrain/world01");

		// setup an array to speed up the look up for index based on name
		worldTileNames = new Dictionary<string, int>();
		for(int ti = 0; ti < worldTiles.Length; ti++) {
			worldTileNames[worldTiles[ti].name] = ti;
		}

		/*GameObject go = new GameObject();
		SpriteRenderer sc = go.AddComponent("SpriteRenderer") as SpriteRenderer;
		sc.sprite = myTextures[0];
		*/
	}
	
	// Update is called once per frame
	void Update () {
	
	}



	void MakeTile(int tileIndex, Vector2 tilePos) {
		GameObject newTile = Instantiate(TilePrefab, tilePos, Quaternion.identity) as GameObject;
		newTile.GetComponent<SpriteRenderer>().sprite = worldTiles[tileIndex];
		newTile.name = worldTiles[tileIndex].name;
	}


	void MakeTile(string tileName, Vector2 tilePos) {

		int tileIndex = 0;
		bool keyExists = worldTileNames.TryGetValue(tileName, out tileIndex);

		if (keyExists) {
			MakeTile(tileIndex, tilePos);
		} else {
			Debug.LogWarning(tileName + " Tile name does not exists.");
		}
	}

}
